package com.tcfvic.client.panels.widgets.widgetcontents;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.tcfvic.client.panels.widgets.TCFWidgetContent;
import com.tcfvic.client.pojos.Person;
import com.tcfvic.client.tools.Tools;

public class EducationLanguageTab extends AbstractDataEntryWidgets implements
		TCFWidgetContent {

	private ListBox lbDriversLicense = new ListBox();
	private ListBox lbLicense1 = new ListBox();
	private ListBox lbLicense2 = new ListBox();
	private ListBox lbLicense3 = new ListBox();
	private ListBox lbLicense4 = new ListBox();
	private ListBox lbEngList = new ListBox();
	private ListBox lbEngSpeak = new ListBox();
	private ListBox lbEngRead = new ListBox();
	private ListBox lbEngWrite = new ListBox();

	@Override
	public Widget getContent() {
		// Create a table to layout the form options
		FlexTable ft = new FlexTable();
		ft.setCellSpacing(4);
		ft.setWidth("100%");
		
		ft.setHTML(6, 0, "Licenses");
		FlexTable ftLicense = new FlexTable();
		lbDriversLicense = getLicenseList();
		ftLicense.setHTML(0, 1, "Licence 1");
		ftLicense.setWidget(0, 2, lbDriversLicense);
		lbLicense1 = getLicenseList();
		ftLicense.setHTML(1, 1, "Licence 2");
		ftLicense.setWidget(1, 2, lbLicense1);
		lbLicense2 = getLicenseList();
		ftLicense.setHTML(2, 1, "Licence 3");
		ftLicense.setWidget(2, 2, lbLicense2);
		lbLicense3 = getLicenseList();
		ftLicense.setHTML(1, 3, "Licence 4");
		ftLicense.setWidget(1, 4, lbLicense3);
		lbLicense4 = getLicenseList();
		ftLicense.setHTML(2, 3, "Licence 5");
		ftLicense.setWidget(2, 4, lbLicense4);
		
		ft.setWidget(7, 1, ftLicense);
		ft.getFlexCellFormatter().setColSpan(7, 1, 2);
		
		ft.setHTML(14, 0, "Language ability<br>(self-assessment)");
		lbEngList = getBegIntAdvList();
		ft.setHTML(15, 1, "English listening");
		ft.setWidget(15, 2, lbEngList);
		lbEngSpeak = getBegIntAdvList();
		ft.setHTML(16, 1, "English speaking");
		ft.setWidget(16, 2, lbEngSpeak);
		lbEngRead = getBegIntAdvList();
		ft.setHTML(17, 1, "English reading");
		ft.setWidget(17, 2, lbEngRead);
		lbEngWrite = getBegIntAdvList();
		ft.setHTML(18, 1, "English writing");
		ft.setWidget(18, 2, lbEngWrite);

		saveButton.getElement().setId("saveButton");
		ft.setWidget(19, 0, saveButton);

		return ft;
	}

	private ListBox getBegIntAdvList() {
		ListBox lb = new ListBox();
		lb.addItem("");
		lb.addItem("Beginner");
		lb.addItem("Intermediate");
		lb.addItem("Advanced");
		return lb;
	}

	private ListBox getLicenseList() {
		ListBox lb = new ListBox();
		lb.addItem("");
		lb.addItem("Car");
		lb.addItem("Crane");
		lb.addItem("Criminal history check");
		lb.addItem("Forklift");
		lb.addItem("Motorbike");
		lb.addItem("Test and Tag");
		lb.addItem("Truck");
		lb.addItem("Working with Children");
		lb.addItem("Working safely at heights");
		lb.addItem("White Card");
		return lb;
	}

	@Override
	public <T> T saveContent(T t) throws Exception {
		if (t != null && t instanceof Person) {
			Person person = (Person) t;

			try {
				person.setDriverslicense(lbDriversLicense.getItemText(lbDriversLicense
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setLicense1(lbLicense1.getItemText(lbLicense1
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setLicense2(lbLicense2.getItemText(lbLicense2
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setLicense3(lbLicense3.getItemText(lbLicense3
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setLicense4(lbLicense4.getItemText(lbLicense4
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setEnglishListening(lbEngList.getItemText(lbEngList
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setEnglishSpeaking(lbEngSpeak.getItemText(lbEngSpeak
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setEnglishReading(lbEngRead.getItemText(lbEngRead
						.getSelectedIndex()));
			} catch (Exception e) {
			}
			try {
				person.setEnglishWriting(lbEngWrite.getItemText(lbEngWrite
						.getSelectedIndex()));
			} catch (Exception e) {
			}
		}
		return t;
	}

	@Override
	public void updateContent(Object o) {
		clear();
		if (o instanceof Person) {
			Person person = (Person) o;

			lbDriversLicense.setSelectedIndex(Tools.getSelectedIndex(lbDriversLicense,
					person.getDriverslicense()));
			lbLicense1.setSelectedIndex(Tools.getSelectedIndex(lbLicense1,
					person.getLicense1()));
			lbLicense2.setSelectedIndex(Tools.getSelectedIndex(lbLicense2,
					person.getLicense2()));
			lbLicense3.setSelectedIndex(Tools.getSelectedIndex(lbLicense3,
					person.getLicense3()));
			lbLicense4.setSelectedIndex(Tools.getSelectedIndex(lbLicense4,
					person.getLicense4()));
			lbEngList.setSelectedIndex(Tools.getSelectedIndex(lbEngList, person
					.getEnglishListening()));
			lbEngSpeak.setSelectedIndex(Tools.getSelectedIndex(lbEngSpeak,
					person.getEnglishSpeaking()));
			lbEngRead.setSelectedIndex(Tools.getSelectedIndex(lbEngRead, person
					.getEnglishReading()));
			lbEngWrite.setSelectedIndex(Tools.getSelectedIndex(lbEngWrite,
					person.getEnglishWriting()));
		}

	}

	@Override
	public void clear() {
		lbDriversLicense.setSelectedIndex(0);
		lbLicense1.setSelectedIndex(0);
		lbLicense2.setSelectedIndex(0);
		lbLicense3.setSelectedIndex(0);
		lbLicense4.setSelectedIndex(0);
		lbEngList.setSelectedIndex(0);
		lbEngSpeak.setSelectedIndex(0);
		lbEngRead.setSelectedIndex(0);
		lbEngWrite.setSelectedIndex(0);

	}

	@Override
	public String getName() {
		return "Education/Language";
	}

	@Override
	public void initialise() {
		// TODO Auto-generated method stub

	}

	@Override
	public void createNewRecord() {
		// TODO Auto-generated method stub
		
	}


}
